(in-package :lilu-clim)

(in-package :climacs)

(defun %edit-file (thing &rest args
                  &key new-process (process-name "Climacs") (width 900) (height 400)
                    (text-style *climacs-text-style*))
  "Edit THING in an existing climacs process or start a new one. THING
can be a filename (edit the file) or symbol (edit its function definition)."
  (declare (ignore process-name width height text-style new-process))
  (let ((climacs-frame (find-climacs-frame))
        (command
         (typecase thing
           (null nil)
           (symbol (list 'drei-lisp-syntax::com-edit-definition thing))
           ((or string pathname)
            (truename thing)  ; raise file-error if file doesn't exist
            (list 'esa-io::com-find-file thing))
           (t (error 'type-error :datum thing
                     :expected-type '(or null string pathname symbol))))))
    (if climacs-frame
        (when command
          (execute-frame-command climacs-frame command))
        (apply #'climacs-common command args)))
  t)

(clim-listener::define-listener-command (com-select-debugger :name t)
    nil
  (lilu-desk:select-default-debugger))

(in-package :lilu-clim)

(defmethod lilu-app:run-open-repl ((application clim-application) &key error-p wait-p)
  (declare (ignore error-p))
  (clim-listener:run-listener :new-process (null wait-p) :package :lilu-desk))

(defmethod lilu-app:run-open-editor ((application clim-application) &key file nline error-p wait-p)
  (declare (ignore error-p))
  (if file
      (climacs::%edit-file file :new-process (null wait-p))
      (climacs:climacs :new-process (null wait-p))))

(defun make-clim-debugger-hook ()
  (lilu-debugger:make-standard-hook (:clim condition me-or-my-encapsulation)
    (clim-debugger:debugger condition
                            me-or-my-encapsulation)))

(defvar *clim-debugger-hook* (make-clim-debugger-hook))

(defmethod lilu-app:get-debugger-hook ((application clim-application))
  *clim-debugger-hook*)

(defmethod lilu-app:run-open-inspector ((application clim-application) object &key error-p wait-p)
  (clouseau:inspect object :new-process (not wait-p) :handle-errors error-p))

